

//冲本地缓存中cookie中取出petname的值


var petname  =$.cookie('petname')

$('#ask').click(function(){
	petname? location.href='ask.html':location.href='login.html'
})

//判断有没有petname决定 user图片的样式和行为

if(petname){
	
	$('#user').find('span').last().text(petname)
}else{
	$('#user').find('span').last().text('登录').end().end.removeAttr('data-toggle').click(function(){
		location.href='login.html'
		
	})
}
//获取首页数据

$.get('/question/all',function(resData){
	console.log(resData)
	console.log(resData.length)
	console.log(resData[0].content)
	console.log(resData[0].ip)
	console.log(resData[0].time)

	var messages='' 
	for(var i=0;i<resData.length;i++){
		var msg =resData[i]
		messages +='<section>'
		messages +='<div><strong>' +msg.petname  +'</strong></div>'		
		messages +='<p>' +msg.content +'</p>'
		messages+= '<p>' + formatTime(msg.time) +'<span>&#x3000;'+  formatIp(msg.ip) +'</span>'+'</p>'
		messages+='</section>'
	}
	
	
$('.questions').html(messages).css('width','50%')
	
	
})


function formatTime(time){
	//把字符串转化成时间对象
			var now =new Date(time)
			//获取年月日时分秒
			var year =now.getFullYear()
			var month =now.getMonth()
			var day =now.getDate()
			var hour =now.getHours()
			var minute=now.getMinutes()
			
			month =month<10?'0'+month:month
			day =day<10?'0'+day:day
			hour=hour<10?'0' +hour:hour
			minute=minute<10?'0' +minute:minute
		
			//返回拼接后的时间
			return year +'-' +month +'-' +day +'  '+hour+':' +minute
		
}
	




function formatIp(ip){
	
	if(ip.startsWith('::1')){
		return '127.0.0.1'
	}
	if(ip.startsWith('::ffff:')){
	return ip.substr(7)
	}
	
}











